﻿#include "plcworker.h"
#include <QDebug>
#include <QThread>

PlcWorker::PlcWorker(QObject *parent) : QObject(parent)
{
    m_bPlcConnect = false;
    m_serialPort = new QSerialPort();//实例化串口类一个对象
    if (m_serialPort->isOpen())//如果串口已经打开了 先给他关闭了
    {
        m_serialPort->clear();
        m_serialPort->close();
    }
    QStringList m_serialPortName;
    foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
    {
        m_serialPortName << info.portName();
    }

    if (m_serialPortName.size() > 0){
        //设置串口名字 假设我们上面已经成功获取到了 并且使用第一个
        m_serialPortName[0] = "COM2";
        m_serialPort->setPortName(m_serialPortName[0]);
        if (!m_serialPort->open(QIODevice::ReadWrite))//用ReadWrite 的模式尝试打开串口
        {
            qDebug() << m_serialPortName[0] << "打开失败!";
            return;
        }
        else{
            // 连接信号槽 当下位机发送数据QSerialPortInfo 会发送个 readyRead 信号, 我们定义个槽void receiveInfo()解析数据
            connect(m_serialPort, SIGNAL(readyRead()), this, SLOT(receiveInfo()));
            m_serialPort->setBaudRate(QSerialPort::Baud19200, QSerialPort::AllDirections);//设置波特率和读写方向
            m_serialPort->setDataBits(QSerialPort::Data8);		//数据位为8位
            m_serialPort->setFlowControl(QSerialPort::NoFlowControl);//无流控制
            m_serialPort->setParity(QSerialPort::EvenParity);	//偶校验位
            m_serialPort->setStopBits(QSerialPort::OneStop); //一位停止位
            start();
        }
    }
    else {
        qDebug() << "serialPort erro!";
    }

}

PlcWorker::~PlcWorker()
{

}

void PlcWorker::Initialize()
{

}

void PlcWorker::start()
{
    // PLC 需要进行一次开始指令
    QByteArray sPlcHeart = "CR";
    sendInfo(sPlcHeart);
}


void PlcWorker::sendInfo(QByteArray sendBuf)
{
    QByteArray endBuf = "\r";
    sendBuf = sendBuf + endBuf;
    //qDebug() << sendBuf;
    m_serialPort->write(sendBuf);////这句是真正的给单片机发数据 用到的是QIODevice::write 具体可以看文档
}

void PlcWorker::receiveInfo()
{
    if (m_bReceive)
    {
        return;
    }

    QByteArray info = m_serialPort->readAll();
    //qDebug() << "receiveInfo" <<info;
    if (info == "CC\r\n"){
        m_bPlcConnect = true;
        return;
    }
    if (info == "E1\r\n"){
        m_bPlcConnect = false;
        return;
    }
    if (info == "OK\r\n"){
        return;
    }

    std::string str = info.toStdString();
    int pos = str.find("\r");
    str = str.substr(0,pos);
    int n = atoi(str.c_str());

    switch(m_nSig){
    case ENCODER:
    case INCODERA:
        m_nINCODERA = n;
        m_nENCODER = n;
        break;
    case MOTOR1_REV:
        m_nMOTOR1_REV = n;
    default:
    case PCCD_1: m_nPCCD_1 = n;
        break;
    case PCCD_2: m_nPCCD_2 = n;
        break;
    case PCCD_3: m_nPCCD_3 = n;
        break;
    case PCCD_4: m_nPCCD_4 = n;
        break;
    case PCCD_5: m_nPCCD_5 = n;
        break;
    case PCCD_6: m_nPCCD_6 = n;
        break;
    case PCCD_7: m_nPCCD_7 = n;
        break;
    case PCCD_8: m_nPCCD_8 = n;
        break;
    case MOTOR_NG1:m_nMOTOR_NG1 = n;
        break;
    case MOTOR_NG2:m_nMOTOR_NG2 = n;
        break;
    case MOTOR_NG3:m_nMOTOR_NG3 = n;
        break;
    case MOTOR_NG4:m_nMOTOR_NG4 = n;
        break;
    case MOTOR_NG5:m_nMOTOR_NG5 = n;
        break;
    case MOTOR_NG6:m_nMOTOR_NG6 = n;
        break;
    case BLOW_TIME:m_nBLOW_TIME = n;
        break;
    case FALL_TIME:m_nFALL_TIME = n;
        break;
    case WARN_TIME:m_nWARN_TIME = n;
        break;
    case STOP_TIME:m_nSTOP_TIME = n;
        break;
    case INCODERB:m_nINCODERB = n;
        break;

    case TOUCH_CCD1A:m_nTOUCH_CCD1A = n;
        break;
    case TOUCH_CCD1B:m_nTOUCH_CCD1B = n;
        break;
    case TOUCH_CCD2A:m_nTOUCH_CCD2A = n;
        break;
    case TOUCH_CCD2B:m_nTOUCH_CCD2B = n;
        break;
    case TOUCH_CCD3A:m_nTOUCH_CCD3A = n;
        break;
    case TOUCH_CCD3B:m_nTOUCH_CCD3B = n;
        break;
    case TOUCH_CCD4A:m_nTOUCH_CCD4A = n;
        break;
    case TOUCH_CCD4B:m_nTOUCH_CCD4B = n;
        break;
    case TOUCH_CCD5A:m_nTOUCH_CCD5A = n;
        break;
    case TOUCH_CCD5B:m_nTOUCH_CCD5B = n;
        break;
    case TOUCH_CCD6A:m_nTOUCH_CCD6A = n;
        break;
    case TOUCH_CCD6B:m_nTOUCH_CCD6B = n;
        break;

    }
}

void PlcWorker::readPlcInfo(SIG_PLC nSig)
{
    m_nSig = nSig;
    if (!m_bPlcConnect){
        return;
    }
    QByteArray start = "RD ";
    QByteArray sendBuf;
    switch (nSig)
    {
    case MOTOR1_REV:sendBuf = "DM202";
        break;
    case ENCODER:
    case INCODERA: sendBuf = "DM0";
        break;
    case PCCD_1:sendBuf = " DM112";
        break;
    case PCCD_2:sendBuf = "DM100";
        break;
    case PCCD_3:sendBuf = "DM114";
        break;
    case PCCD_4:sendBuf = "DM102";
        break;
    case PCCD_5:sendBuf = "DM104";
        break;
    case PCCD_6:sendBuf = "DM106";
        break;
    case PCCD_7:sendBuf = "DM108";
        break;
    case PCCD_8:sendBuf = "DM110";
        break;
    case MOTOR_NG1:sendBuf = "DM130";
        break;
    case MOTOR_NG2:sendBuf = "DM132";
        break;
    case MOTOR_NG3:sendBuf = "DM134";
        break;
    case MOTOR_NG4:sendBuf = "DM136";
        break;
    case MOTOR_NG5:sendBuf = "DM138";
        break;
    case MOTOR_NG6:sendBuf = "DM140";
        break;
    case BLOW_TIME:sendBuf = "DM236";
        break;
    case FALL_TIME:sendBuf = "DM232";
        break;
    case WARN_TIME:sendBuf = "DM230";
        break;
    case STOP_TIME:sendBuf = "DM234";
        break;
    case INCODERB: sendBuf = "DM1";
        break;
    case TOUCH_CCD1A:sendBuf = "DM60";
        break;
    case TOUCH_CCD1B:sendBuf = "DM61";
        break;
    case TOUCH_CCD2A:sendBuf = "DM62";
        break;
    case TOUCH_CCD2B:sendBuf = "DM63";
        break;
    case TOUCH_CCD3A:sendBuf = "DM64";
        break;
    case TOUCH_CCD3B:sendBuf = "DM65";
        break;
    case TOUCH_CCD4A:sendBuf = "DM66";
        break;
    case TOUCH_CCD4B:sendBuf = "DM67";
        break;
    case TOUCH_CCD5A:sendBuf = "DM68";
        break;
    case TOUCH_CCD5B:sendBuf = "DM69";
        break;
    case TOUCH_CCD6A:sendBuf = "DM70";
        break;
    case TOUCH_CCD6B:sendBuf = "DM71";
        break;
    default:
        break;
    }
    sendBuf = start + sendBuf;
    sendInfo(sendBuf);
}

void PlcWorker::writePlcInfo(SIG_PLC nSig, int nIndex)
{
    m_nSig = 0;
    if (!m_bPlcConnect){
        return;
    }
    QByteArray start = "WR ";
    QByteArray sK = " ";
    QByteArray index = QString::number(nIndex).toUtf8();
    QByteArray sendBuf;

    switch (nSig)
    {
    case START:
        sendBuf = "MR100";
        break;
    case STOP:
        sendBuf = "MR101";
        break;
    case MOTOR1:sendBuf = "MR103";
        break;
    case MOTOR2:
        break;
    case INSENSOR:
        break;
    case INPRESSSW1:
        break;
    case INPAUSESW:
        break;
    case QPULSE1:sendBuf = "MR302";
        break;
    case QDIRECTION1:sendBuf = "MR303";
        break;
    case QPULSE2:
        break;
    case QDIRECTION2:
        break;
    case QENABLED1:sendBuf = "MR305";
        break;
    case QENABLED2:
        break;
    case QGRAP1:sendBuf = "MR410";
        break;
    case QGRAP2:sendBuf = "MR306";
        break;
    case QGRAP3:sendBuf = "MR411";
        break;
    case QGRAP4:sendBuf = "MR307";
        break;
    case QGRAP5:sendBuf = "MR308";
        break;
    case QGRAP6:sendBuf = "MR309";
        break;
    case QGRAP7:sendBuf = "MR310";
        break;
    case QGRAP8:sendBuf = "MR311";
        break;
    case QNG1OUTAIR:sendBuf = "MR312";
        break;
    case QNG2OUTAIR:sendBuf = "MR313";
        break;
    case QNG3OUTAIR:sendBuf = "MR314";
        break;
    case QNG4OUTAIR:sendBuf = "MR315";
        break;
    case QNG5OUTAIR:sendBuf = "MR400";
        break;
    case QNG6OUTAIR:sendBuf = "MR401";
        break;
    case QPLATE:sendBuf = "MR402";
        break;
    case QINAIR:sendBuf = "MR403";
        break;
    case QLINE:sendBuf = "MR404";
        break;
    case QTIMEOUT_LINE:sendBuf = "MR405";
        break;
    case QLIGHTG:sendBuf = "MR408";
        break;
    case QLIGHTY:sendBuf = "MR407";
        break;
    case QLIGHTR:sendBuf = "MR406";
        break;
    case QLIGHTVOX:sendBuf = "MR409";
        break;
    case MOTOR1_REV:sendBuf = "DM202";
        break;
    case MOTOR2_REV:sendBuf = "DM212";
        break;
    case MOTOR_RATION1:
        break;
    case MOTOR_RATION2:
        break;
    case OPTICAL:
        break;
	case PCCD_1:sendBuf = " DM112";
		break;
	case PCCD_2:sendBuf = "DM100";
		break;
	case PCCD_3:sendBuf = "DM114";
		break;
	case PCCD_4:sendBuf = "DM102";
		break;
	case PCCD_5:sendBuf = "DM104";
		break;
	case PCCD_6:sendBuf = "DM106";
		break;
	case PCCD_7:sendBuf = "DM108";
		break;
	case PCCD_8:sendBuf = "DM110";
		break;
    case MOTOR_NG1:sendBuf = "DM130";
        break;
    case MOTOR_NG2:sendBuf = "DM132";
        break;
    case MOTOR_NG3:sendBuf = "DM134";
        break;
    case MOTOR_NG4:sendBuf = "DM136";
        break;
    case MOTOR_NG5:sendBuf = "DM138";
        break;
    case MOTOR_NG6:sendBuf = "DM140";
        break;
    case RESULT_NUMBER:sendBuf = "DM96";
        break;
    case CAMERA_RESULT:sendBuf = "DM98";
        break;
    case WARN_TIME:sendBuf = "DM230";
        break;
    case STOP_TIME:sendBuf = "DM234";
        break;
    case BLOW_TIME:sendBuf = "DM236";
        break;
    case FALL_TIME:sendBuf = "DM232";
        break;
    case CUT_STATE:sendBuf = "DM240";
        break;
    default:
        break;
    }
    sendBuf = start + sendBuf + sK + index;
    sendInfo(sendBuf);
}


void PlcWorker::writePlcInfo(SIG_PLC nSig, float nIndex)
{
    int nIndex1 = 0;
    int nIndex2 = 0;
    splitFloat(nIndex, nIndex1, nIndex2);

    QByteArray start = "WR ";
    QByteArray sK = " ";

    QByteArray index1 = QString::number(nIndex1).toUtf8();
    QByteArray index2 = QString::number(nIndex2).toUtf8();
    QByteArray sendBuf1;
    QByteArray sendBuf2;
    switch (nSig)
    {
    default:
        break;
    }
    sendBuf1 = start + sendBuf1 + sK + index1;
    sendBuf2 = start + sendBuf2 + sK + index2;
    sendInfo(sendBuf1);
    sendInfo(sendBuf2);
}

void PlcWorker::splitFloat(float fIndex, int& nIndex1, int& nIndex2){
    float fData;
    fData = fIndex;
    nIndex1 = (int)fIndex;
    nIndex2 = (int)((fData - nIndex1) * 10);
}
